﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logistics_Service.DTO;
using System.Data;

namespace Logistics_Service.DAO
{
    public class ChucVuDAO
    {
        public ChucVuDTO[] DanhSachChucVu()
        {
            ChucVuDTO[] arr;
            string query = "select * from ChucVu";
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            int n = dt.Rows.Count;
            arr = new ChucVuDTO[n];
            for (int i = 0; i < n; i++)
            {
                object cv = GetDataFromDataRow(dt, i);
                arr[i] = (ChucVuDTO)cv;
            }
            return arr;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            ChucVuDTO cv = new ChucVuDTO();
            cv.Ma = dt.Rows[i]["Ma"].ToString();
            cv.Ten = dt.Rows[i]["Ten"].ToString();
            cv.LoaiNhanVien = LoaiNhanVienDAO.Get(dt.Rows[i]["MaLoaiNhanVien"].ToString());
            cv.TienLuong = float.Parse(dt.Rows[i]["TienLuong"].ToString());           
            cv.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
            return (object)cv;
        }
        public static ChucVuDTO Get(string ma)
        {
            string query = "select * from ChucVu where ma = '" + ma + "'";
            DataProvider provider = new DataProvider();
            ChucVuDTO cv = new ChucVuDTO();
            DataTable dt = provider.ExecuteQuery(query);
            cv.Ma = dt.Rows[0]["Ma"].ToString();
            cv.Ten = dt.Rows[0]["Ten"].ToString();
            cv.LoaiNhanVien = LoaiNhanVienDAO.Get(dt.Rows[0]["MaLoaiNhanVien"].ToString());
            cv.TienLuong = float.Parse(dt.Rows[0]["TienLuong"].ToString());
            cv.DaXoa = bool.Parse(dt.Rows[0]["DaXoa"].ToString());            

            return cv;
        }
    }
}